/**
 * 
 */
package jgame.Admin;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import jgame.StoryTeller.*;

/**
 * @author Justin
 *
 */
public class JGameContentReader {
	private BufferedReader reader;
	private ArrayList<String> CONTENT;
	private ArrayList<JGameStorySet> SETS;
	
	public JGameContentReader(String content_file)
	{
		CONTENT = new ArrayList<String>();
		SETS = new ArrayList<JGameStorySet>();
		ArrayList<String> set_stuff = new ArrayList<String>();
		
		String content = "", setID = "", setTitle = "";
		
		
		try {reader = new BufferedReader(new FileReader(getClass().getClassLoader().getResource(".").getPath() + "jgame/Resources/" + content_file));} 
		catch (FileNotFoundException e) {System.out.println("Content File Not Found");	e.printStackTrace();}
		
		try {while ((content = reader.readLine())!=null)CONTENT.add(content);}
		catch (IOException e) {System.out.println("Error Reading Content File");	e.printStackTrace();}
		
		//This is for formatting purposes when writing game content.  Tabs will help a writer keep track of their work (ie which lines are sets, scenes, and branches).
		for(int j = 0; j<CONTENT.size(); j++)
		{CONTENT.set(j, CONTENT.get(j).replaceAll("\t", ""));}
		
		for(int i = 0; i<CONTENT.size(); i++)
		{			
			if(CONTENT.get(i).substring(0,1).equals("@"))
			{
				if(i>0)
					SETS.add(new JGameStorySet(Integer.parseInt(setID), setTitle, set_stuff));
					
				set_stuff.clear();
				
				setID = CONTENT.get(i).substring(1,4);		setTitle = CONTENT.get(i).substring(5,CONTENT.get(i).length());
			}	
			
			else
				set_stuff.add(CONTENT.get(i));
			
			if(i==CONTENT.size()-1)
				SETS.add(new JGameStorySet(Integer.parseInt(setID), setTitle, set_stuff));
		}
	}
	
	public ArrayList<String> getContentString()
	{	return CONTENT;	}
	
	public void test()
	{
		for(int i = 0; i<SETS.size(); i++)
			SETS.get(i).test();
	}
	
	public JGameStorySet getSet(int state)
	{
		while (state>999) state/=1000;
		
		for(int i = 0; i<SETS.size(); i++)
		{			
			if(SETS.get(i).getExtID()==state)
				return SETS.get(i);
		}
		
		return null;
	}
	
}
